package com.tg.data.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.appbase.custom.base.AiSeviceInfo;
import com.appbase.custom.base.CarServerData;
import com.appbase.custom.base.DeviceNoticeBean;
import com.appbase.custom.base.RtcIceServer;
import com.appbase.custom.base.ServiceBought;
import com.appbase.custom.base.ServiceInfo;
import com.appbase.custom.constant.DeviceStateConstants;
import com.google.gson.annotations.SerializedName;
import com.tange.base.toolkit.StringUtils;
import com.tange.core.data.structure.Device;
import com.tange.core.device.manage.DeviceInfoBasic;
import com.tange.core.device.manage.DeviceOnlineStatus;
import com.tange.core.device.manage.DeviceThumbnail;
import com.tange.feature.data.structure.DeviceBasicResp;
import com.tange.feature.data.structure.DeviceBatteryResp;
import com.tange.feature.data.structure.DeviceLocationResp;
import com.tg.data.helper.DeviceTypeHelper;
import com.tg.data.http.entity.DeviceServiceStatusBean;

import java.util.List;

import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.annotation.Transient;

/**
 *   设备列表信息类
 */
@Entity
@Deprecated() // 后续迭代准备废弃此类，太大杂烩。
public class DeviceItem implements Parcelable {

    public static final int USER_BIND = 1;

    private static final String CAR_LEVEL_PRIMARY = "primary"; //基础
    private static final String CAR_LEVEL_STANDARD = "standard"; //标准
    private static final String CAR_LEVEL_PROFESSIONA = "professiona"; //专业


    @Transient
    public DeviceBasicResp deviceBasicResp;

    @Transient
    public DeviceBatteryResp deviceBatteryResp;

    @Transient
    public DeviceLocationResp deviceLocationResp;

    @Transient
    public DeviceServiceStatusBean deviceServiceResp;

    @Id
    public long id;//设备ID
    public String uuid;//设备UUID

    public String p2p_type;

    public String p2p_id; //设备P2PID
    public String password;
    @Transient
    public int actived;//激活标记(未激活1，已激活2)
    @Transient
    public int is_online;
    public String name;
    public String image_path;
    @Transient
    public int bind_type; //1:绑定 2：分享
    @Transient
    public String des_key;  //des解密key
    @Transient
    public String update_status; //当前设备更新状态
    @Transient
    public String update_progress; //当前设备更新进度
    @Transient
    public String current_version;//当前设备版本码
    public String current_version_code; //当前设备版本号
    public String firmware_id;//当前设备固件id
    @Transient
    public DeviceNoticeBean device_notice;
    @Transient
    public String foreign_iccid_notice;
    public long start_time;

    public int preconnect = 0;
    @Transient
    public int gps_model;//
    @Transient
    public int low_battery = -1;

    @Transient
    public double lat;
    @Transient
    public double lng;
    @Transient
    public double speed;

    @Transient
    public CarServerData car_server_data;
    @Transient
    public ServiceBought server_bought; //是否购买过云服务

    @Transient
    public String timezone; //时区
    @Transient
    public int is_upgrade; //固件是否有升级  1：有新的固件更新，0  无
    @Transient
    public String device_type;//摄像机类型
    public int is_open = 1;//是否已打开，默认打开
    @Transient
    public String iccid;//4g卡号

    @Transient
    public String connect_way;//联网类型，lan:网线，sim:4g
    @Transient
    public String p2p_platform;
    @Transient
    public boolean about_to_expire;
    @Transient
    public int notice;
    public int qoe = -1;
    public String tg_relay;
    @Transient
    public boolean is_directional;

    public boolean should_show_super_ai;

    public String attrs;
    @Transient
    public String country;
    @Transient
    public String city;

    public String abilities;

    public int charging = -1;

    public int orderId = 0;
    public String orderUrl;
    public int unReadCount = 0;
    public boolean showUnRead = false;
    public long callTime;
    public boolean has_doorlock_pwd = false;

    public String mode;

    public boolean suportWechat = false;

    @SerializedName("foreign_service_hook")
    public String foreignServiceHook;

    @Transient
    @SerializedName("tg_webrtc")
    public List<RtcIceServer> iceServers;


    public boolean isSuportWechat(){
        return suportWechat;
    }

    protected DeviceItem(Parcel in) {
        id = in.readLong();
        uuid = in.readString();
        p2p_id = in.readString();
        password = in.readString();
        actived = in.readInt();
        is_online = in.readInt();
        name = in.readString();
        image_path = in.readString();
        bind_type = in.readInt();
        des_key = in.readString();
        update_status = in.readString();
        update_progress = in.readString();
        current_version = in.readString();
        current_version_code = in.readString();
        firmware_id = in.readString();
        device_notice = in.readParcelable(DeviceNoticeBean.class.getClassLoader());
        preconnect = in.readInt();
        gps_model = in.readInt();
        low_battery = in.readInt();
        lat = in.readDouble();
        lng = in.readDouble();
        speed = in.readDouble();
        server_data = in.readParcelable(ServiceInfo.class.getClassLoader());
        ai_server_data = in.readParcelable(AiSeviceInfo.class.getClassLoader());
        sim_server_data = in.readParcelable(AiSeviceInfo.class.getClassLoader());
        car_server_data = in.readParcelable(CarServerData.class.getClassLoader());
        server_bought = in.readParcelable(ServiceBought.class.getClassLoader());
        timezone = in.readString();
        is_upgrade = in.readInt();
        device_type = in.readString();
        is_open = in.readInt();
        iccid = in.readString();
        connect_way = in.readString();
        p2p_platform = in.readString();
        about_to_expire = in.readByte() != 0;
        notice = in.readInt();
        foreign_iccid_notice = in.readString();
        start_time = in.readLong();
        qoe = in.readInt();
        is_directional = in.readByte() != 0;
        tg_relay = in.readString();
        should_show_super_ai = in.readByte() != 0;
        attrs = in.readString();
        country = in.readString();
        city = in.readString();
        abilities = in.readString();
        charging = in.readInt();

        orderId = in.readInt();
        orderUrl = in.readString();
        unReadCount = in.readInt();
        showUnRead = in.readByte() != 0;

        callTime = in.readLong();
        has_doorlock_pwd = in.readByte() != 0;

        mode = in.readString();
        foreignServiceHook = in.readString();

        suportWechat = in.readByte() != 0;
        iceServers = in.createTypedArrayList(RtcIceServer.CREATOR);
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(uuid);
        dest.writeString(p2p_id);
        dest.writeString(password);
        dest.writeInt(actived);
        dest.writeInt(is_online);
        dest.writeString(name);
        dest.writeString(image_path);
        dest.writeInt(bind_type);
        dest.writeString(des_key);
        dest.writeString(update_status);
        dest.writeString(update_progress);
        dest.writeString(current_version);
        dest.writeString(current_version_code);
        dest.writeString(firmware_id);
        dest.writeParcelable(device_notice, flags);
        dest.writeInt(preconnect);
        dest.writeInt(gps_model);
        dest.writeInt(low_battery);
        dest.writeDouble(lat);
        dest.writeDouble(lng);
        dest.writeDouble(speed);
        dest.writeParcelable(server_data, flags);
        dest.writeParcelable(ai_server_data, flags);
        dest.writeParcelable(sim_server_data, flags);
        dest.writeParcelable(car_server_data, flags);
        dest.writeParcelable(server_bought, flags);
        dest.writeString(timezone);
        dest.writeInt(is_upgrade);
        dest.writeString(device_type);
        dest.writeInt(is_open);
        dest.writeString(iccid);
        dest.writeString(connect_way);
        dest.writeString(p2p_platform);
        dest.writeByte((byte) (about_to_expire ? 1 : 0));
        dest.writeInt(notice);
        dest.writeString(foreign_iccid_notice);
        dest.writeLong(start_time);
        dest.writeInt(qoe);
        dest.writeByte((byte) (is_directional ? 1 : 0));
        dest.writeString(tg_relay);
        dest.writeByte((byte) (should_show_super_ai ? 1 : 0));
        dest.writeString(attrs);
        dest.writeString(country);
        dest.writeString(city);
        dest.writeString(abilities);
        dest.writeInt(charging);

        dest.writeInt(orderId);
        dest.writeString(orderUrl);
        dest.writeInt(unReadCount);
        dest.writeByte((byte) (showUnRead ? 1 : 0));
        dest.writeLong(callTime);
        dest.writeByte((byte) (has_doorlock_pwd ? 1 : 0));
        dest.writeString(mode);
        dest.writeString(foreignServiceHook);

        dest.writeByte((byte) (suportWechat ? 1 : 0));
        dest.writeTypedList(iceServers);

    }

    @Override
    public int describeContents() {
        return 0;
    }


    public boolean isHasCustomerMessage(){
        return unReadCount > 0;
    }


    public static final Creator<DeviceItem> CREATOR = new Creator<DeviceItem>() {
        @Override
        public DeviceItem createFromParcel(Parcel in) {
            return new DeviceItem(in);
        }

        @Override
        public DeviceItem[] newArray(int size) {
            return new DeviceItem[size];
        }
    };



    public int getPreconnect() {
        return preconnect;
    }

    public boolean isPreconnect() {
        return preconnect == 1;
    }

    public void setPreconnect(int preconnect) {
        this.preconnect = preconnect;
    }


    public boolean isCarPrimary() {
        return car_server_data != null && CAR_LEVEL_PRIMARY.equals(car_server_data.level);
    }

    public boolean isStandard() {
        return car_server_data != null && CAR_LEVEL_STANDARD.equals(car_server_data.level);
    }

    public boolean isProfessiona() {
        return car_server_data != null && CAR_LEVEL_PROFESSIONA.equals(car_server_data.level);
    }


    public ServiceInfo getServer() {
        return server_data;
    }

    public boolean canConnect(){
        return !is4GDevice() || is_online == DeviceStateConstants.DEVICE_ONLINE;
    }

    @Transient
    public ServiceInfo server_data;//云存储服务信息
    @Transient
    public AiSeviceInfo ai_server_data;//人形人脸服务信息

    public AiSeviceInfo getSimServerData() {
        return sim_server_data;
    }

    @Transient
    public AiSeviceInfo sim_server_data;//人形人脸服务信息

    public CarServerData getCarServerData() {
        return car_server_data;
    }


    public String getConnectWay() {
        return connect_way;
    }

    public boolean isShare() {
        return bind_type == 2;
    }

    public DeviceNoticeBean getDevice_notice() {
        return device_notice;
    }

    public DeviceItem() {
    }

    public DeviceItem(DeviceInfoBasic deviceInfoBasic) {
        this.uuid = deviceInfoBasic.getDeviceId();
        this.name = deviceInfoBasic.getDeviceName();
        this.abilities = deviceInfoBasic.getDeviceAbilities();
        this.attrs = deviceInfoBasic.getDeviceAttributes();
        this.bind_type = deviceInfoBasic.isOwner() ? 1 : 2;
        deviceBasicResp = new DeviceBasicResp();
    }

    public void setDeviceInfoBasic(DeviceInfoBasic deviceInfoBasic) {
        this.uuid = deviceInfoBasic.getDeviceId();
        this.name = deviceInfoBasic.getDeviceName();
        this.abilities = deviceInfoBasic.getDeviceAbilities();
        this.attrs = deviceInfoBasic.getDeviceAttributes();
        this.bind_type = deviceInfoBasic.isOwner() ? 1 : 2;
        deviceBasicResp = new DeviceBasicResp();
    }

    public void setDeviceOnlineStatus(DeviceOnlineStatus deviceOnlineStatus) {
        if (deviceOnlineStatus != null){
            this.is_online = Boolean.TRUE.equals(deviceOnlineStatus.isOnline()) ? 1 : 0;
        }
    }

    public void setDeviceThumbnail(DeviceThumbnail deviceThumbnail) {
        this.image_path = deviceThumbnail.getUrl();
    }

    public boolean isOnline() {
        return is_online == DeviceStateConstants.DEVICE_ONLINE;
    }

    public boolean isCar() {
        return DeviceTypeHelper.isCar(device_type);
    }

    public boolean isBirdFeeder() {
        return DeviceTypeHelper.isBirdFeeder(device_type);
    }

    public boolean isLamp() {
        return DeviceTypeHelper.isLamp(device_type);
    }

    public boolean isPassiveDoorBellDevice(){
        return DeviceTypeHelper.isPassiveDoorBellDevice(device_type);
    }

    public int getCustomerMessageCount(){
        return unReadCount;
    }

    public boolean isShowUnRead(){
        return showUnRead && !StringUtils.isEmpty(orderUrl);
    }

    public boolean isBatteryDevice() {
        return DeviceTypeHelper.isBatteryDevice(device_type);
    }

    public boolean isLockBell() {
        return DeviceTypeHelper.isLockBell(device_type);
    }

    public boolean isWifiDevice() {
        return !TextUtils.isEmpty(device_type)&&device_type.contains(DeviceTypeHelper.DEVICE_WIFI);
    }

    public boolean isBatteryWifiDevice() {
        return DeviceTypeHelper.isBatteryWifiDevice(device_type);
    }

    public boolean is4GDevice() {
        if (StringUtils.isEmpty(this.attrs)){
            return false;
        }
        JSONObject jsonObject = JSONObject.parseObject(this.attrs);
        if (jsonObject != null){
            String network = jsonObject.getString("network");
            return StringUtils.equalsIgnoreCase(network, "sim");

        }
        return false;
    }

    public String getCity() {
        return city;
    }

    public String getCountry() {
        return country;
    }

    public String getCategory(){
        if (StringUtils.isEmpty(this.attrs)){
            return "";
        }
        JSONObject jsonObject = JSONObject.parseObject(this.attrs);
        if (jsonObject != null){
            return jsonObject.getString("category");

        }
        return "";
    }

    public static Device createBasicDevice(DeviceItem deviceItem) {
        Device device = new Device();
        device.setId(deviceItem.id);
        device.setUuid(deviceItem.uuid);
        device.setName(deviceItem.name);
        device.setProductType(deviceItem.device_type);
        device.setCover(deviceItem.image_path);
        device.setAbilities(deviceItem.abilities);
        device.setAttributes(deviceItem.attrs);
        device.setPassword(deviceItem.password);
        device.setP2pId(deviceItem.p2p_id);
        device.setP2pPlatform(deviceItem.p2p_platform);
        device.setDecryptKey(deviceItem.des_key);
        device.setPreConnect(deviceItem.preconnect);
        device.setRelay(deviceItem.tg_relay);
        device.setOnlineValue(deviceItem.is_online);
        device.setBindType(deviceItem.bind_type);
        device.setP2pType(deviceItem.p2p_type);
        device.setIceServers(deviceItem.iceServers);
        return device;
    }

    public String generateString() {
        return "DeviceItem{" +
                "id=" + id +
                ", uuid='" + uuid + '\'' +
                ", p2p_id='" + p2p_id + '\'' +
                ", password='" + password + '\'' +
                ", actived=" + actived +
                ", is_online=" + is_online +
                ", name='" + name + '\'' +
                ", image_path='" + image_path + '\'' +
                ", bind_type=" + bind_type +
                ", des_key='" + des_key + '\'' +
                ", update_status='" + update_status + '\'' +
                ", update_progress='" + update_progress + '\'' +
                ", current_version='" + current_version + '\'' +
                ", current_version_code='" + current_version_code + '\'' +
                ", firmware_id='" + firmware_id + '\'' +
                ", device_notice=" + device_notice +
                ", foreign_iccid_notice='" + foreign_iccid_notice + '\'' +
                ", start_time=" + start_time +
                ", preconnect=" + preconnect +
                ", gps_model=" + gps_model +
                ", foreignServiceHook = " + foreignServiceHook +
                ", low_battery=" + low_battery +
                ", lat=" + lat +
                ", lng=" + lng +
                ", speed=" + speed +
                ", car_server_data=" + car_server_data +
                ", server_bought=" + server_bought +
                ", timezone='" + timezone + '\'' +
                ", is_upgrade=" + is_upgrade +
                ", device_type='" + device_type + '\'' +
                ", is_open=" + is_open +
                ", iccid='" + iccid + '\'' +
                ", connect_way='" + connect_way + '\'' +
                ", p2p_platform='" + p2p_platform + '\'' +
                ", about_to_expire=" + about_to_expire +
                ", notice=" + notice +
                ", qoe=" + qoe +
                ", tg_relay='" + tg_relay + '\'' +
                ", is_directional=" + is_directional +
                ", should_show_super_ai=" + should_show_super_ai +
                ", attrs='" + attrs + '\'' +
                ", country='" + country + '\'' +
                ", city='" + city + '\'' +
                ", abilities='" + abilities + '\'' +
                ", charging=" + charging +
                ", server_data=" + server_data +
                ", ai_server_data=" + ai_server_data +
                ", sim_server_data=" + sim_server_data +
                ", orderUrl =" + orderUrl +
                ", unReadCount =" + unReadCount +
                ", showUnRead =" + showUnRead +
                ", supoortWebchat =" + suportWechat +
                '}';
    }
}
